package com.xc.videosample

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.datasource.HttpDataSource
import androidx.media3.exoplayer.ExoPlayer
import com.xc.videosample.databinding.ActivityExoVideoBinding

class ExoActivity : AppCompatActivity() {

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, ExoActivity::class.java))
        }
    }
     // 重定向地址 http://baobab.kaiyanapp.com/api/v1/playUrl?vid=2178&editionType=default&source=aliyun

    var videoUri =
        "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=2178&editionType=default&source=aliyun"

    lateinit var dataBinding: ActivityExoVideoBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        dataBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_exo_video)

        val player = ExoPlayer.Builder(this).build()
        player.addListener(videoListener)
        player.setVideoSurfaceView(dataBinding.playerView)

        dataBinding.btnStart.setOnClickListener {
            // Build the media item.
            val mediaItem = MediaItem.fromUri(videoUri)
            // Set the media item to be played.
            player.setMediaItem(mediaItem)
            // Prepare the player.
            player.prepare()
            // Start the playback.
            player.play()
            //播放模式
            player.repeatMode = Player.REPEAT_MODE_ONE
        }
    }

    private val videoListener = object : Player.Listener {
        override fun onPlaybackStateChanged(playbackState: Int) {
            super.onPlaybackStateChanged(playbackState)
            when (playbackState) {
                //这是初始状态、播放器停止时以及播放失败时的状态。在此状态下，播放器将仅保留有限的资源。
                Player.STATE_IDLE -> {

                }
                //播放器无法立即从当前位置开始播放。这主要是因为需要加载更多数据。
                Player.STATE_BUFFERING -> {

                }
                //播放器能够从当前位置立即播放。
                Player.STATE_READY -> {

                }
                //播放器完整播放了所有媒体。
                Player.STATE_ENDED -> {

                }
            }
        }

        override fun onIsPlayingChanged(isPlaying: Boolean) {
            super.onIsPlayingChanged(isPlaying)
            Log.i("chyy", " onIsPlayingChanged : $isPlaying")

        }

        override fun onPlayerError(error: PlaybackException) {
            super.onPlayerError(error)
            val cause = error.cause
            Log.i("chyy", " error : ${error.message}")
            if (cause is HttpDataSource.HttpDataSourceException) {
                // An HTTP error occurred.
                val httpError = cause
                // It's possible to find out more about the error both by casting and by querying
                // the cause.
                if (httpError is HttpDataSource.InvalidResponseCodeException) {
                    // Cast to InvalidResponseCodeException and retrieve the response code, message
                    // and headers.
                } else {
                    // Try calling httpError.getCause() to retrieve the underlying cause, although
                    // note that it may be null.
                }
            }
        }

    }


}